const { readFileSync, writeFileSync } = require('fs')

;((async () => {
    const rawData = readFileSync('./ares.txt').toString()
    const regions = rawData.split('\n').map(line => {
      const [ provinceCode, province, cityCode, city, areaCode, area ] = line.split('	')
      return {
          province, city, area, provinceCode, cityCode, areaCode
      }
    })

    const provinces =
        regions.map(region => {
          const { province, provinceCode } = region
          return {
            province,
            provinceCode
          }
        }).reduce((pre, cur) => {
          const { province, provinceCode } = cur
          if (pre.has(province)) {
            const currentCode = pre.get(province)
            if (currentCode !== provinceCode) {
              // 冲突了 一个省份有两个code
              // console.log(province + ' error')
            }
          } else {
            pre.set(province, provinceCode)
          }
          return pre
        }, new Map())

    const citys =
        regions.map(region => {
          const { city, cityCode } = region
          return {
            city,
            cityCode
          }
        }).reduce((pre, cur) => {
          const { city, cityCode } = cur
          if (pre.has(city)) {
            const currentCode = pre.get(city)
            if (currentCode !== cityCode) {
              // 冲突了 一个省份有两个code
              // console.log(city + ' error')
            }
          } else {
            pre.set(city, cityCode)
          }
          return pre
        }, new Map())

    const areas =
        regions.map(region => {
          const { city, cityCode, area, areaCode } = region
          return {
            city,
            cityCode,
            area,
            areaCode
          }
        }).reduce((pre, cur) => {
          const { city, area, areaCode } = cur
          const key = `${city}${area.trim()}`
          // console.log(key)
          if (pre.has(key)) {
            const currentCode = pre.get(key)
            if (currentCode !== areaCode) {
              // 冲突了 一个地区有两个code
              console.log(key, ' areas error')
            }
          } else {
            pre.set(key, areaCode)
          }
          return pre
        }, new Map())


    const provincesCode = Array.from(provinces.keys()).map(key => {
      const province = key
      const provinceCode = provinces.get(province)
      const code = `put("${province}", ${provinceCode});`
      return code
    }).join('\n')

    const citysCode = Array.from(citys.keys()).map(key => {
      const city = key
      const cityCode = citys.get(city)
      const code = `put("${city}", ${cityCode});`
      return code
    }).join('\n')


    const areasCode = Array.from(areas.keys()).map(key => {
      const areasCode = areas.get(key)
      const code = `put("${key}", ${areasCode});`
      return code
    }).join('\n')

    writeFileSync('./province', provincesCode)
    writeFileSync('./city', citysCode)
    writeFileSync('./area', areasCode)

})())